package com.herui.iot.ui;

import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;


import com.herui.sdyu_lib.base.BaseActivity;
import com.herui.sdyu_lib.utils.LogUtils;
import com.herui.sport_care.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import butterknife.ButterKnife;

/**
 * Created by amanda.shan on 2021/10/13.
 */
public abstract class BaseSportActivity extends BaseActivity {

    private final int REQUEST_CODE = 1;

    private List<String> needPermission;
    private final int REQUEST_CODE_PERMISSION = 0;

    protected void setTitle( String title) {
        TextView textView = findViewById(R.id.tv_title);
        if (textView != null){
            textView.setText(title);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        View view = findViewById(R.id.imgv_back);
        if (view != null) {
            view.setOnClickListener(v -> {
                onBackPressed();
                LogUtils.i("back");
            });
        }
    }

    public void askMultiplePermission(String[] permissionArray) {
        needPermission = new ArrayList<>();
        for (String permissionName :
                permissionArray) {
            if (!checkIsAskPermission(this, permissionName)) {
                needPermission.add(permissionName);
            }
        }

        if (needPermission.size() > 0) {
            //开始申请权限
            ActivityCompat.requestPermissions(this,
                    needPermission.toArray(new String[needPermission.size()]),
                    REQUEST_CODE_PERMISSION);
        } else {
            //获取数据
        }

    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case REQUEST_CODE_PERMISSION:
                Map<String, Integer> permissionMap = new HashMap<>();
                for (String name :
                        needPermission) {
                    permissionMap.put(name, PackageManager.PERMISSION_GRANTED);
                }

                for (int i = 0; i < permissions.length; i++) {
                    permissionMap.put(permissions[i], grantResults[i]);
                }
                if (checkIsAskPermissionState(permissionMap, permissions)) {
                    //获取数据
                    checkBLE();
                } else {
                    //提示权限获取不完成，可能有的功能不能使用
                }

                break;
            default:
        }
    }

    /**
     * 检测蓝牙设备是否开启
     *
     * @return
     */
    public boolean checkBLE() {

        BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
//        if (!bluetoothAdapter.isEnabled()) {
//            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
//            startActivityForResult(enableBtIntent, REQUEST_CODE);
//            return false;
//        } else {
//            return true;
//        }
        bluetoothAdapter.isEnabled();
        return true;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE) {
//            if (BluetoothUtils.isBluetoothEnabled()) {
//            } else {
//
//            }
        }
    }

    public boolean checkIsAskPermission(Context context, String permission) {
        if (ContextCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
            return false;
        } else {
            return true;
        }

    }

    public boolean checkIsAskPermissionState(Map<String, Integer> maps, String[] list) {
        for (int i = 0; i < list.length; i++) {
            if (maps.get(list[i]) != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
        return true;

    }
}
